//import pathToRegexp from 'path-to-regexp';
import { getIndexListAllApi, getOpenid, thirdLogin, recommend, userinfo, thirdSina, loginAPi, goCartAPi } from './api';
import { Toast } from 'antd-mobile';

export default {
  namespace: 'home',
  state: {
    carouselList: [],
    menuList: [],
    contentList: [],
    floorList: [],
    relGoodsRecommedlist: [],
    list: [],
    getOpenidList: [],
    userinfoList: []
  },
  reducers: {
    save(state, { data }) {
      return { ...state, ...data };
    },
  },
  effects: {
    *loginEFF({ obj }, { put, call }) {
      Toast.loading('加载中...', 3);
      const result = yield call(loginAPi, obj);
      if (result.code == 200) {
        yield call(goCartAPi)
      }
      return result
    },
    *getIndexListAllEFF({ }, { put, call }) {
      Toast.loading('加载中...', 3);
      const result = yield call(getIndexListAllApi);
      if (result.code == 200) {
        yield put({ type: 'save', data: result.ret });
        Toast.hide();
      } else {
        Toast.fail(result.msg, 1.5);
      }
    },
    *getOpenidEFF({ obj }, { put, call }) {
      Toast.loading('加载中...', 3);
      const result = yield call(getOpenid, obj);
      if (result.code == 200) {
        yield put({ type: 'save', data: { getOpenidList: result.ret || [] } });
        Toast.hide();
      } else {
        Toast.fail(result.msg, 1.5);
      }
    },
    *thirdLoginEFF({ obj }, { put, call }) {
      Toast.loading('加载中...', 3);
      const result = yield call(thirdLogin, obj);
      return result;
    },
    *thirdSinaEFF({ obj }, { put, call }) {
      Toast.loading('加载中...', 3);
      const result = yield call(thirdSina, obj);
      return result;
    },
    *userinfoEFF({ obj }, { put, call }) {
      Toast.loading('加载中...', 3);
      const result = yield call(userinfo, obj);
      if (result.code == 200) {
        yield put({ type: 'save', data: { userinfoList: result.ret || [] } });
        Toast.hide();
      } else {
        Toast.fail(result.msg, 1.5);
      }
    },
    *recommendEFF({ }, { put, call }) {
      //Toast.loading('加载中...', 3);
      const result = yield call(recommend);
      if (result.code == 200) {
        yield put({ type: 'save', data: result.ret });
        // Toast.hide();
      } else {

      }
    }
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        // console.log(pathname)
        if (pathname == '/') {
          dispatch({ type: 'getIndexListAllEFF' }).then(() => {
            dispatch({ type: 'recommendEFF' });
          });

        }
      });
    },
  },
};
